#if !defined(AFX_MYINFODETECTOR_H__64E4A69E_BB58_425D_830C_ADD1760E29A4__INCLUDED_)
#define AFX_MYINFODETECTOR_H__64E4A69E_BB58_425D_830C_ADD1760E29A4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "FastAlloc.h"
#include "User.h"
#include "TimerManager.h"
#include "ClientProfileManager.h"
#include "DebugManager.h"
#include "RegexpHandler.h"

class MyinfoDetector : public Flags, public FastAlloc<MyinfoDetector>
{
public:
	MyinfoDetector() {}
	~MyinfoDetector() {}
	
	void runDetect(User::Ptr user){
		if(user){
			
			int64_t tick = GET_TICK();
			StringMap params;
			MyinfoProfile::List lst = ClientProfileManager::getInstance()->getMyinfoProfiles(params);

			for(MyinfoProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
				MyinfoProfile& cq = *i;	

				string version, extraVersion, formattedTagExp, verTagExp;
				string tagExp = cq.getTag();

				verTagExp = RegexpHandler::formatRegExp(cq.getTag(), params);

				formattedTagExp = verTagExp;
				string::size_type j = formattedTagExp.find("%[version]");
				if(j != string::npos) {
					formattedTagExp.replace(j, 10, ".*");
				}
				
				string extTagExp = cq.getExtendedTag();
				string formattedExtTagExp = extTagExp;
				j = extTagExp.find("%[version2]");
				if(j != string::npos) {
					formattedExtTagExp.replace(j, 11, ".*");
				}

				DETECTION_DEBUG("Checking profile to nick, " + user->getNick() + " : " + cq.getName());

				if (!RegexpHandler::matchProfile(user->getTag(), formattedTagExp))									{ continue; } 
				if (!RegexpHandler::matchProfile(Util::toString(user->getIconStatus()), cq.getStatus()))				{ continue; }
				if (!RegexpHandler::matchProfile(user->getDescription(), formattedExtTagExp))							{ continue; }
				if (!RegexpHandler::matchProfile(user->getConnection(), cq.getConnection()))							{ continue; }
				if (!RegexpHandler::matchProfile(user->getNick(), cq.getNick()))										{ continue; } 
				if (!RegexpHandler::matchProfile(Util::toString(user->getBytesShared()), cq.getShared()))				{ continue; } 
				if (!RegexpHandler::matchProfile(user->getEmail(), cq.getEmail()))									{ continue; } 

				if (verTagExp.find("%[version]") != string::npos) { version = RegexpHandler::getVersion(verTagExp, user->getTag()); }
				if (extTagExp.find("%[version2]") != string::npos) { extraVersion = RegexpHandler::getVersion(extTagExp, user->getDescription()); }

				if (!(cq.getVersion().empty()) && !RegexpHandler::matchProfile(version, cq.getVersion()))	{ continue; }

				DETECTION_DEBUG("Client found: " + cq.getName() + " time taken: " + Util::toString(GET_TICK()-tick) + " milliseconds");
				user->setClientType(cq.getName());
				if (cq.getUseExtraVersion()) {
					if (extraVersion != "")
						user->setClientType(cq.getName() + " " + extraVersion );
				} else {
					if (version != "")
						user->setClientType(cq.getName() + " " + version);
				}
				if (!cq.getCheatingDescription().empty())
						user->setCheat(cq.getCheatingDescription(), true, false);
				user->setClientComment(cq.getComment());
				user->updated();

				if(cq.getRawToSend() > 0) {
					user->sendRawCommand(cq.getRawToSend(), Client::CONTEXT_RAW_MYINFO);
				}
				return;
			}
		}
	}
	
};

#endif // !defined(AFX_MYINFODETECTOR_H__64E4A69E_BB58_425D_830C_ADD1760E29A4__INCLUDED_)